package com.squash;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import java.util.LinkedList;
import java.util.Queue;


public class SlidingDigitView extends RelativeLayout {

    private Digit currentDigit;
    private Digit nextDigit;

    private int currentScore = 0;

    private Queue<Integer> toBeRemoved = new LinkedList<Integer>();

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


    public SlidingDigitView(Context context) {
        super(context);
        initialize();
    }

    public SlidingDigitView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public SlidingDigitView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    public void initialize()
    {
        params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

        //create the current score view
        currentDigit = new Digit(this.getContext());
        currentDigit.setScore(currentScore);
        currentDigit.setId(currentScore);

        addView(currentDigit, params);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Pull out the Action value from the event for processing
        int action = event.getAction();

        final int currentScoreCopy = currentScore;

        //check for next number to be displayed
        nextDigit = new Digit(this.getContext());
        nextDigit.setScore(currentScore+1);
        nextDigit.setId(currentScore + 1);
        nextDigit.setVisibility(GONE);
        addView(nextDigit,params);

        //check for views to be removed from child list
         if (toBeRemoved.size() != 0)
             removeView(findViewById(toBeRemoved.remove()));


        if(action == MotionEvent.ACTION_DOWN)
        {
            return true;
        }
        else if(action == MotionEvent.ACTION_UP)
        {

            Animation animationDisappear = AnimationUtils.loadAnimation(this.getContext(), R.anim.digit_disappear);
            animationDisappear.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                    findViewById(currentScoreCopy).setVisibility(GONE);
                    toBeRemoved.add(currentScoreCopy);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            Animation animationAppear = AnimationUtils.loadAnimation(this.getContext(),R.anim.digit_appear);
            animationAppear.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    findViewById(currentScoreCopy+1).setVisibility(VISIBLE);

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            findViewById(currentScore).startAnimation(animationDisappear);
            findViewById(currentScore+1).startAnimation(animationAppear);


            currentScore++;

            return true;
        }
        return false;

    }
}
